/*
 * 
 * 
 * 
 */
package com.cms.controller.front;

import com.cms.entity.Category;
import com.cms.entity.Content;
import com.cms.service.ContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Controller - 内容
 * 
 * 
 * 
 */
@Slf4j
@Controller("frontContentController")
@RequestMapping("/content")
public class ContentController extends BaseController {

	@Autowired
	private ContentService contentService;

	/**
	 * 内容
	 */
	@RequestMapping
	public String index(Integer id, ModelMap modelMap) {
		log.info("获取内容信息...");
		Content content = contentService.getById(id);
		modelMap.addAttribute("currentContent", content);
		Category category = content.getCategory();
		modelMap.addAttribute("currentCategory", category);
		return "/templates/"+getCurrentTemplate()+"/"+category.getDetailTemplate();
	}

	/**
	 * 点击数
	 */
	@RequestMapping("/visits")
	public String visits(Integer id) {
		log.info("点击数累加操作...");
		if (id == null) {
			return "0";
		}
		Content content = contentService.getById(id);
		Integer visits = content.getVisits();
		visits = visits+1;
		content.setVisits(visits);
		contentService.updateById(content);
		return "document.write("+visits+")";
	}
}